package com.itheima.edy.info.manager.dao;

import com.itheima.edy.info.manager.domain.Teacher;

public class TeacherDao {
   static Teacher []teachers = new Teacher[5];

    public static boolean addTeacherInfo(Teacher teach) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            //判断对象是否为空  如果为空  把为空的索引赋值给index
            if (teacher == null) {
                index = i;
                break;
            }
        }
            if (index == -1) {
                return false;
            } else {
                //如果为空  把传进来的学生信息传入给数组
                teachers[index] = teach;
                return true;
            }
        }
        public static Teacher[] findAllTeacher(){
            return teachers;
        }

    public static void deleteTeacherById(String delId) {
        //查找id在容器中所在的索引位置
        int index = getIndex(delId);
        //将该索引位置使用null元素进行替换
       teachers[index] = null;
    }

    public static int getIndex(String id){
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if(teachers != null && teacher.getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public static void updateTeacher(String updateId,Teacher newTeach){
        //查找undateId，在容器中索引位置
        int index = getIndex(updateId);
        //将该索引位置，使用新的学生对象替换
        teachers[index] = newTeach;
    }

}
